var album_index_js = {
    psize: 2,
    pno: 1,
    layer: null,
    flow: null,
    laytpl: null,
    layerIndex: null,
    item: null,
    btn_init: function () {
        $('#album_index_add').on('click', function () {
            $.get('/album/add', {}, function(str){
                album_index_js.layerIndex = album_index_js.layer.open({
                    type: 1,
                    content: str,
                    title: '添加',
                    area: ['500px', '500px'],
                    btn: ['提交','关闭']
                    ,yes: function(index, layero){
                        $('#album_add_submit').click();
                        //按钮【按钮一】的回调
                    }
                });
            });
        });
        $('#album_index_info').on('click', function () {
            if (null != album_index_js.item){
                $.get('/photo/index', {}, function (data,status) {
                    $('#album_index_div').hide();
                    $('#index_content').append(data);
                });
            }else {
                album_index_js.layer.msg('先要选中一个',{icon:5});
            }
        });
        $('#album_index_update').on('click', function () {
            if (null != album_index_js.item){
                var _id = album_index_js.item.id;
                $.get('/album/update', {id: _id}, function(str){
                    album_index_js.layerIndex = album_index_js.layer.open({
                        type: 1,
                        content: str,
                        title: '修改',
                        area: ['500px', '500px'],
                        btn: ['提交','关闭']
                        ,yes: function(index, layero){
                            $('#album_add_submit').click();
                        }
                    });
                });
            }else {
                album_index_js.layer.msg('先要选中一个',{icon:5});
            }
        });
        $('#album_index_delete').on('click', function () {
            if (null != album_index_js.item){
                var _id = album_index_js.item.id;
                album_index_js.layer.confirm('真的删除行么', function(index){
                    $.post('/album/delete', {id: _id}, function (data,status) {
                        if (data.state == 'success'){
                            $('#' + _id).remove();
                            album_index_js.layer.msg(data.message,{icon:6});
                        }else {
                            album_index_js.layer.msg(data.message,{icon:5});
                        }
                    });
                    album_index_js.layer.close(index);
                });
            }else {
                album_index_js.layer.msg('先要选中一个',{icon:5});
            }
        });
    },
    flow_init: function () {
        album_index_js.flow.load({
            elem: '#album_index_content',
            done: function (page, next) {
                var lis = '';
                $.post('/album/getByPage', {pno: page, psize: album_index_js.psize}, function (res) {
                    album_index_js.pno = page;
                    var _page = res.count/2;
                    album_index_js.laytpl(album_index_js.tpl).render({data: res.data}, function (html) {
                        lis = html;
                    });
                    next(lis, page < _page);
                })
            }
        });
    },
    item_select: function(that){
        if (null != album_index_js.item){
            $('#' + album_index_js.item.id).removeClass('album-index-item-select');
        }
        $(that).addClass('album-index-item-select');
        album_index_js.item = {id: $(that).attr('id'),
            name: $(that).attr('data-name'),
            introduce: $(that).attr('data-introduce'),
            postDate: $(that).attr('data-postDate'),
            state: $(that).attr('data-state')};
    },
    item_mouseenter: function(div){
        album_index_js.layerIndex = album_index_js.layer.tips($(div).attr('data-postDate'), div, {
            tips: 3
        });
    },
    item_mouseleave: function(){
        album_index_js.layer.close(album_index_js.layerIndex);
    },
    tpl: '{{# layui.each(d.data, function(index, item){ }}\n' +
    '        <div class="album-index-item" id="{{ item.id }}"' +
    '               data-postDate="{{ item.postDate }}" data-introduce="{{ item.introduce }}" data-name="{{ item.name }}" data-state="{{ item.state }}"' +
    '               onmouseenter="album_index_js.item_mouseenter(this)" onmouseleave="album_index_js.item_mouseleave()" onclick="album_index_js.item_select(this)">\n' +
    '            <img src="/static/image/album_icon.png">\n' +
    '            <span class="album-item-text">{{ item.name }}</span>\n' +
    '        </div>\n' +
    '        {{# }); }}'
}